package com.bo.sort;

import java.util.Arrays;

/**
 * 非比较方式排序
 *
 * @Author: gpb
 * @Date: 2023/12/21 11:12
 * @Description:
 */
public class NonComparativeSort {

    public static void main (String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 150;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            countSort(arr1);
            radixSort(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed = false;
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = generateRandomArray(maxSize, maxValue);
        int[] arr2 = copyArray(arr);
        System.out.println("计数排序");
        printArray(arr);
        countSort(arr);
        printArray(arr);
        System.out.println("基数排序");
        printArray(arr2);
        radixSort(arr2);
        printArray(arr2);
    }

    /**
     * 技术排序
     * 时间复杂度：O(N)
     * 空间富足度：O(M)
     * 有稳定性
     *
     * @param arr
     */
    public static void countSort (int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }
        int max = Integer.MIN_VALUE;
        for (int i : arr) {
            max = Math.max(max, i);
        }
        int[] bucket = new int[max + 1];
        // 统计每个数出现的次数
        for (int i : arr) {
            bucket[i]++;
        }
        int index = 0;
        for (int i = 0; i < bucket.length; i++) {
            while (bucket[i]-- > 0) {
                arr[index++] = i;
            }
        }
    }

    /**
     * 基数排序
     * 时间复杂度：O(N)
     * 空间复杂度：O(M)
     * 有稳定性
     *
     * @param arr
     */
    public static void radixSort (int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }
        radixSort(arr, 0, arr.length - 1, maxBits(arr));
    }

    private static void radixSort (int[] arr, int l, int r, int maxBits) {
        // 辅助数组中
        int[] help = new int[r - l + 1];
        // 依次从个位,十位,百位,千位,万位...
        for (int i = 1; i <= maxBits; i++) {
            // 基数数组
            int[] counts = new int[10];
            for (int index = l; index <= r; index++) {
                int digit = getDigit(arr[index], i);
                counts[digit]++;
            }
            // 前缀和
            for (int i1 = 1; i1 < counts.length; i1++) {
                counts[i1] = counts[i1] + counts[i1 - 1];
            }

            for (int index = r; index >= l; index--) {
                int digit = getDigit(arr[index], i);
                help[counts[digit] - 1] = arr[index];
                counts[digit]--;
            }
            int j = 0;
            for (int index = l; index <= r; index++, j++) {
                arr[index] = help[j];
            }
        }
    }

    private static int getDigit (int num, int bit) {
        return ((num / ((int) Math.pow(10, bit - 1))) % 10);
    }

    private static int maxBits (int[] arr) {
        int max = Integer.MIN_VALUE;
        for (int i : arr) {
            max = Math.max(max, i);
        }
        int res = 0;
        while (max != 0) {
            max = max / 10;
            res++;
        }
        return res;
    }


    // for test
    public static void comparator (int[] arr) {
        Arrays.sort(arr);
    }

    // for test
    public static int[] generateRandomArray (int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray (int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual (int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray (int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}
